/**
 * 菜单服务模块
 */

const { addMenu, validPath, updateMenu, deleteMenuById, getMenuList, getMenuListByTeacherId, getMenusByRoleId } = require('../db/menus');

/**
 * 添加菜单
 * @param {*} ctx 上下文
 */
const add = async (ctx) => {
    try {
        const menu = ctx.request.body;
        const result = await validPath(menu);
        if (result) {
            return ctx.body = {
                code: 0,
                msg: '已存在相同的路径'
            }
        }
        await addMenu(menu);
        ctx.body = {
            code: 1,
            msg: '添加成功'
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '添加失败'
        }
    }
}

/**
 * 修改菜单
 * @param {*} ctx 
 */
const update = async (ctx) => {
    try {
        const result = await updateMenu(ctx.request.body);
        if (result === 0) {
            return ctx.body = {
                code: 0,
                msg: '已存在相同的路径'
            }
        }
        ctx.body = {
            code: 1,
            msg: '菜单修改成功'
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '菜单修改失败'
        }
    }
}

/**
 * 删除菜单
 * @param {*} ctx 
 */
const deleteMenu = async (ctx) => {
    try {
        await deleteMenuById(ctx.params.id);
        ctx.body = {
            code: 1,
            msg: '菜单删除成功'
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '菜单删除失败'
        }
    }
}

/**
 * 获取菜单列表
 * @param {*} ctx 
 */
const getList = async (ctx) => {
    try {
        const list = await getMenuList();
        ctx.body = {
            code: 1,
            data: list
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '菜单列表获取失败'
        }
    }
}

/**
 * 根据教师ID获取菜单列表
 * @param {*} ctx 
 */
const getListByTeacherId = async (ctx) => {
    try {
        const list = await getMenuListByTeacherId(ctx.state.user.id);
        for (let i = list.length - 1; i >= 0; i--) {
            const item = list[i];
            if (item.pid !== 0) continue;
            if (!list.some(c => c.pid === item.id)) {
                list.splice(i, 1);
            }
        }
        ctx.body = {
            code: 1,
            data: list
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '菜单列表获取失败'
        }
    }
}

/**
 * 根据角色ID获取菜单列表
 * @param {*} ctx 
 */
const getListByRoleId = async ctx => {
    try {
        const list = await getMenusByRoleId(ctx.request.params.id);
        ctx.body = {
            code: 1,
            data: list.map(c => c.menu_id)
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '菜单列表获取失败'
        }
    }
}

module.exports = {
    add,
    update,
    getList,
    deleteMenu,
    getListByRoleId,
    getListByTeacherId
}